import util from '../util/index.js'
import config from './user.config.js'

let result = new Object();

result.config = config;
// {"data":{"directCode":null,"email":null,"fileIdOfAvatar":517218363994117,"genderCode":"male","gmtCreate":null,"gmtModified":1708410422490,"id":511387221413893,"introduction":"","inviteCode":null,"inviteUserId":null,"isInit":true,"isTemporary":null,"nickname":"方某方","phone":null,"userBalance":[{"balanceCode":"totalConsumption","gmtCreate":null,"gmtModified":null,"id":511387221413893,"number":1.00,"statusCode":null,"userId":511387221413893}],"userBalanceList":[{"balanceCode":"totalConsumption","gmtCreate":null,"gmtModified":null,"id":511387221413893,"number":1.00,"statusCode":null,"userId":511387221413893}],"userOnline":null,"userRoleList":[],"username":null},"errMsg":null,"msg":"获取成功","msgDictCode":"getSuccess","operationCode":null,"state":2000,"stateCode":"SUCCESS","success":true}
/**
 * 获取在线属性
 */
result.getIsOnline = (user) => {
    if (user.userOnline == null) {
        return false;
    }
    if (user.userOnline.isOnline == null) {
        return false;
    }
    if (!user.userOnline.isOnline) {
        return false;
    }
    let gmtLastHeartbeatTime = user.userOnline.gmtLastHeartbeat;
    // 获取当前时间
    let now = new Date().getTime();
    // 如果心跳时间超过了3分钟，则认为不在线
    if (now - gmtLastHeartbeatTime > 1000 * 60 * 3) {
        return false;
    }
    return true;
}
/**
 * 格式化成简易格式
 */
result.formatSimple = (user) => {
    let newUser = util.newJSON(user);
    let roleCodes = [];
    let roleIs = new Object();

    // 获取userConfig中的roles对象
    let roles = config.roles;
    // 获取所有的key
    let roleKeys = Object.keys(roles);
    // 遍历所有的key
    for (let i = 0; i < roleKeys.length; i++) {
        // 获取roleCode的首字母大写后的字符串
        let roleIsKey = "isRole" + util.firstLetterUpper(roleKeys[i]);
        roleIs[roleIsKey] = false;
    }

    if (user.userRoleList != null) {
        for (let i = 0; i < user.userRoleList.length; i++) {
            let roleCode = user.userRoleList[i].roleCode;
            roleCodes.push(roleCode);
            // 获取roleCode的首字母大写后的字符串
            let roleIsKey = "isRole" + util.firstLetterUpper(roleCode);
            // 将roleCode的首字母大写后的字符串作为key，将roleCode作为value
            roleIs[roleIsKey] = true;
        }
    }

    // 获取isOnline属性
    newUser["isOnline"] = result.getIsOnline(user);
    if (user.userOnline != null) {

        // 获取最后一次在线时间
        newUser["gmtLastOnline"] = user.userOnline.gmtLastOnline;
        // 获取最后一次离线时间
        newUser["gmtLastOffline"] = user.userOnline.gmtLastOffline;
        // 获取最后一次心跳时间
        newUser["gmtLastHeartbeat"] = user.userOnline.gmtLastHeartbeat;
        // 如果存在是否隐身属性
        if (user.userOnline.isInvisible != null) {
            newUser["isInvisible"] = user.userOnline.isInvisible;
        }else{
            newUser["isInvisible"] = false;
        }
    }
    // 删除掉userRoleList属性
    delete newUser.userRoleList;
    // 如果存在userOnlineList属性，则删除掉
    if (newUser.userOnline != null) {
        delete newUser.userOnline;
    }

    // 将余额进行转换，如score，可直接使用user.balance.score便可获取余额
    if (newUser.userBalance != null) {
        newUser.balance = {
            totalConsumption: 0,
            monthConsumption: 0,
            integral: 0,
        };
        for (let i = 0; i < newUser.userBalance.length; i++) {
            newUser.balance[newUser.userBalance[i].balanceCode] = newUser.userBalance[i].number;
        }
        delete newUser.userBalance;
    }


    return {
        ...newUser,
        ...roleIs,
        roleCodes: roleCodes,
    }

}

export default result;


